package servlet;

import pojo.News;
import pojo.NewsPage;
import service.NewsService;
import service.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 郝雷蒙
 * @Description
 * @date 2020/11/10 14:52
 */
public class GetAllNews extends HttpServlet {
    private NewsService newsService = new NewsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");

        Map<String, Object> params = new HashMap<>();


        String page = req.getParameter("page");
        String size = req.getParameter("size");
        String name = req.getParameter("name");
        String startTime = req.getParameter("startTime");
        String afterTime =req.getParameter("afterTime");





        Integer pid = (page==null)? 1 : Integer.parseInt(page);
        Integer num = (size==null)? 5: Integer.parseInt(size);

        params.put("page",pid);
        params.put("size",num);

        if (startTime != null && !startTime.equals("")) {
            params.put("startTime", startTime);
        }

        if (name != null && !name.equals("")) {
            params.put("name", name);
        }
        if (afterTime != null && !afterTime.equals("")) {
            params.put("afterTime", afterTime);
        }
        NewsPage newsPage = newsService.getAllnews(params);
             req.setAttribute("newsPage",newsPage);
             req.setAttribute("name",name);
             req.setAttribute("startTime",startTime);
             req.setAttribute("afterTime",afterTime);

        req.getRequestDispatcher("all_news.jsp").forward(req, resp);
    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }
}
